# -*- coding: utf-8 -*-
from datetime import datetime, timedelta

from odoo import models, fields, api,_
from odoo.exceptions import ValidationError, UserError
from odoo.tools import DEFAULT_SERVER_DATE_FORMAT, DEFAULT_SERVER_DATETIME_FORMAT

class HREmployee1(models.Model):
    _inherit = 'hr.employee'

    def get_employee_over_time_date(self,date):
        report_id=self.env['hr.overtime'].search([('employee_id','=',self.id),('apply_date','=',date),('real_state','=','done')])
        if report_id:
            return report_id.real_period
        else:
            return 0

    def get_employee_over_time_date_by_type(self,start,end,type_id):
        report_ids = self.env['hr.overtime'].search([('employee_id', '=', self.id), ('apply_date', '>=', start),('apply_date','<=',end),('type_id','=',type_id.id),('real_state','=','done')])
        if report_ids:
            return sum(r.real_period for r in report_ids)
        else:
            return 0

    def get_employee_attendance(self,date):
        """
        判断要不要上班
        """
        calendar_id=self.get_current_calendar_id(date)
        if not calendar_id:
            return False
        attendances = calendar_id.attendance_ids.filtered(
            lambda x: x.dayofweek == str(date.weekday()))
        if not attendances:
            # 如果不上班看看有没有调休
            h_setting = self.env['hr.holidays.setting'].search(
                [('sheet_type', '=', '1'), ('date', '=', date)])
            if h_setting:
                attendances = calendar_id.attendance_ids.filtered(
                    lambda x: x.dayofweek == '0')
                return attendances
        if attendances:
            holiday=self.env['hr.holidays.setting'].search(
                [('sheet_type', '=', '2'), ('date', '=', date)])
            if holiday:
                return False
        return attendances

